<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>成绩认定新增</el-breadcrumb-item>
    </el-breadcrumb>
    <el-form :inline="true" class="user-search">
      <el-form-item label="搜索：">
        <el-select v-model="pageQuery.termId" clearable placeholder="学期">
            <el-option :label="item.describes" :value="item.id" v-for="item in termList" :key="item.id"></el-option>
          </el-select>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
      </el-form-item>
    </el-form>
    <el-table size="small" :data="userList" highlight-current-row v-loading="loading" border element-loading-text="拼命加载中" style="width: 100%;">
      <!-- <el-table-column align="center" type="selection" width="60">
      </el-table-column> -->
      <el-table-column align="center" prop="termName" label="学期" width="200">
      </el-table-column>
      <el-table-column align="center" prop="studentName" label="学生姓名" width="150">
      </el-table-column>
      <el-table-column align="center" prop="courseName" label="课程名" width="150">
      </el-table-column>
      <el-table-column align="center" prop="teacherName" label="教师名" width="150">
      </el-table-column>
      <el-table-column align="center" prop="className" label="班级" width="200">
      </el-table-column>
      <el-table-column align="center" prop="scores" label="成绩（分）" width="200">
        <template slot-scope="scope">
          {{scope.row.scores * 100 | 0}}
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" min-width="200">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="addReal(scope.row)">新增认定</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageQuery" :total="total" @callFather="callFather"></Pagination>
    <el-dialog title="成绩录入" :visible.sync="realDialog" width="40%" @click="realDialog=false">
      <el-form label-width="120px" ref="editForm">
        <el-form-item label="新课程：" prop="recordQuery.studentName">
          <el-select v-model="editForm.newCourseId" clearable placeholder="新课程" @change="courseChange">
            <el-option :label="item.courseName" :value="item.id" v-for="item in courseList" :key="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="新分数：" prop="recordQuery.className">
          <el-input-number v-model="editForm.newScores" :min="0" :max="100"></el-input-number>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="realDialog=false">取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="addRealApi">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import Pagination from '../../components/Pagination'
export default {
  name:'UserDetail',
  data() {
    return {
        realDialog: false,
      gradeList: [], // 列表
      userList: [],
      courseList: [],
      pageQuery: {
          termId: '',
        pageNum: 1,
        pageSize: 10
      },
      editForm: {
          oldScoresId: '',
          newCourseId: '',
          newCourseName: '',
          newScores: ''
      },
      total: 0,
      loading: false, //是显示加载
    }
  },
  // 注册组件
  components: {
    Pagination
  },
  created() {
      this.getTermList()
    //   this.getReal()
    this.getCourseList()
  },
  computed: {
    ...mapState(['user'])
  },
  methods: {
      /**
       * 下拉选择新课程
       */
      courseChange (e) {
         this.editForm.newCourseName = this.courseList.filter(item => {
             return item.id == e
         })[0].courseName
      },
      /**
     * 获取课程列表
     */
    getCourseList() {
        let query = {
            pageNum: 1,
            pageSize: -1
        }
      this.$api.course.getCourseList(query).then(res => {
        if (res.code === 200) {
          this.courseList = res.data.records
        }
      }).catch(err => this.loading = false)
    },
      search () {
          this.managerGetGradeList()
      },
      /**
     * 获取学期列表
     */
    getTermList() {
      let query = {
        pageNum: 1,
        pageSize: -1
      }
      this.$api.course.getTermList(query).then(res => {
        if (res.code === 200) {
          this.termList = res.data.records
          let curTermId = res.data.records.filter(item => item.status)[0].id
            this.pageQuery.termId = curTermId
            this.managerGetGradeList()
        }
      })
    },
    /**
     * 管理员查询成绩
     */
    managerGetGradeList () {
      this.$api.course.managerGetGradeList(this.pageQuery).then(res => {
        if (res.code === 200) {
          this.userList = res.data
          // this.total = res.data.total
        }
      })
    },
      /**
       * 审核请求
       */
    checkRecord (id, type) {
        let params = {
            changeId: id,
            sign: type
        }
        this.$api.course.checkRecord(params).then(res => {
            if (res.code === 200) {
                this.getReal()
            }
        })
    },
    /**
     * 认定分数列表
     */
    getReal () {
        this.loading = true
      this.$api.auth.getReal(this.pageQuery).then(res => {
        if (res.code === 200) {
          this.gradeList = res.data.records
          this.total = res.data.total
          this.loading = false
        }
      }).catch(err => this.loading = false)
    },
    /**
     * 增加 认定
     */
    addReal (row) {
        this.realDialog = true
        this.editForm.oldScoresId = row.id
    },
    addRealApi () {
      this.editForm.newScores = this.editForm.newScores / 100
        this.$api.auth.addReal(this.editForm).then(res => {
            if (res.code === 200) {
                this.$message({
                    type: 'success',
                    message: '新增成功'
                })
                this.realDialog = false
            } else {
                this.realDialog = false
            }
        })
    }, 
    /**
     * 分页插件事件
     */
    callFather(parm) {
      this.pageQuery.pageNum = parm.currentPage
      this.pageQuery.pageSize = parm.pageSize
      this.getReal()
    }
  }
}
</script>

<style>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

 
 